<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input v-model="params.name" placeholder="请输入内容" class="search-main" clearable @clear="handleClear" />
      <el-button type="primary" @click="getBuildingList">查询</el-button>

    </div>
    <el-button class="add-building" type="primary" @click="addBuilding">添加楼宇</el-button>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="getList"
      >
        <el-table-column
          type="index"
          label="序号"
        />
        <el-table-column
          label="楼宇名称"
          width="180"
          prop="name"
        />
        <el-table-column
          prop="floors"
          label="层数"
        />
        <el-table-column
          label="在管面积(m²)"
          width="120"
          prop="area"
        />
        <el-table-column
          label="物业费(元/m²)"
          width="120"
          prop="propertyFeePrice"
        />
        <el-table-column
          label="状态"
          prop="status"
          :formatter="statusFormatter"
        />
        <el-table-column
          label="操作"
        >
          <template #default="{row}">
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(row.id)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

    </div>
    <!-- //弹窗 -->
    <div class="from">
      <el-dialog :title="id?'编辑楼宇':'添加楼宇'" :visible="dialogFormVisible" width="30%" @close="close">
        <el-form ref="form" :model="form" class="elFrom" :rules="rules" label-width="120px">
          <el-form-item label="楼宇名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="楼宇层数" :label-width="formLabelWidth" prop="floors">
            <el-input v-model="form.floors" autocomplete="off" />
          </el-form-item>
          <el-form-item label="在管面积" :label-width="formLabelWidth" prop="area">
            <el-input v-model="form.area" autocomplete="off" />
          </el-form-item>
          <el-form-item label="物业费" :label-width="formLabelWidth" prop="propertyFeePrice">
            <el-input v-model="form.propertyFeePrice" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitAdd">{{ id?"编辑":"添加" }}</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
// import buildingAdd from './buildingAdd.vue'
import { getBuildingListAPI, getBuildingDetailAPI, addBuildingAPI, editBuildingAPI, deleteBuildingAPI } from '@/api/building'
export default {
  name: 'Building',
  // components: { buildingAdd },
  data() {
    return {
      // 楼宇列表
      dialogFormVisible: false,

      id: null,
      formData: {},
      getList: [],
      params: {
        name: '',
        page: 1,
        pageSize: 10
      },
      // 表单数据
      form: {
        name: '',
        floors: '',
        area: '',
        propertyFeePrice: ''
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入在管面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入物业费', trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px'

    }
  },
  mounted() {
    // 获取楼宇列表
    this.getBuildingList()
  },
  methods: {
    // 封装获取楼宇列表的方法
    async getBuildingList() {
      const res = await getBuildingListAPI(this.params)
      this.getList = res.data.rows
    },
    // 清空搜索框
    handleClear() {
      // 清空搜索框
      this.params.name = ''
      // 重新获取楼宇列表
      this.getBuildingList()
    },
    // 添加楼宇列表
    addBuilding() {
      this.id = null
      this.dialogFormVisible = true
      // console.log(this.id)
    },
    // 编辑楼宇列表
    async handleEdit(id) {
      this.id = id
      this.dialogFormVisible = true
      const res = await getBuildingDetailAPI(id)
      this.form = res.data
    },
    // 格式化状态
    statusFormatter(row) {
      const MAP = {
        1: '租赁中',
        0: '空置中'
      }
      return MAP[row.status]
    },
    // 删除楼宇列表
    async handleDelete(id) {
      await deleteBuildingAPI(id)
      this.$message.success('删除成功')
      this.getBuildingList()
    },
    // 取消关闭弹窗并清空表单
    cancel() {
      this.id = null
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
    },
    // 提交添加
    submitAdd() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          // 如果有id则编辑，没有则添加
          if (this.id) {
            const { name, floors, area, propertyFeePrice } = this.form
            await editBuildingAPI({ name, floors, area, propertyFeePrice, id: this.id })
            this.$message.success('编辑成功')
          } else {
            const { name, floors, area, propertyFeePrice } = this.form
            await addBuildingAPI({ name, floors, area, propertyFeePrice })
            this.$message.success('添加成功')
          }
          this.getBuildingList()// 调用父组件的获取楼宇列表方法
          this.dialogFormVisible = false
          this.id = null
        }
      })
    },
    // 关闭弹窗,并清空表单
    close() {
      this.dialogFormVisible = false
      this.$refs.form.resetFields()
      this.$emit('update:dialogFormVisible', false)
    }

  }

}
</script>

<style lang="scss" scoped>
.department-container{
  padding:10px;
}
.search-container{
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237,237,237,.9);;
  padding-bottom: 20px;
  .search-label{
    width:100px;
  }
  .search-main{
    width: 220px;
    margin-right: 10px;
  }

}
 .add-building{

    margin-top: 20px;
  }
</style>
